package com.ethan.junit.modules.organization;

import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ethan.common.orm.Page;
import com.ethan.modules.organization.model.User;
import com.ethan.modules.organization.service.UserService;

public class TestOrganization {
	private static BeanFactory bf;
	private static UserService userService;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		bf = new ClassPathXmlApplicationContext("applicationContext.xml");
		userService = (UserService)bf.getBean("userService");
	}
	
	@Test
	public void saveUser() {
		for(int i=10;i<=10;i++){
			User user = new User();
			user.setId(new Long(Integer.valueOf(i)));
			user.setPassword("1234");
			user.setEmail("ethan198827_"+i+"@163.com");
			user.setIsActive(1);
			user.setLoginName("admin"+i);
			user.setOrderNo(i);
			user.setUserName("测试管理用户"+i);
			userService.save(user);			
		}
	}
	@Test
	public void deleteUser(){
		userService.deleteUser(1l);
	}
	
	@Test
	public void findAllUsers(){
		Page<User> page = new Page<User>();
		page.setPageNo(2);
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = userService.findPage(page, "from User");
		List<User> list = page.getResult();
		for(User user : list){
			System.out.println(user.getId()+"========="+user.getUserName());
		}
		System.out.println("++++++++page.getTotalCount++++++++"+page.getTotalCount());
		System.out.println("++++++++page.getTotalPages++++++++"+page.getTotalPages());
	}
	
	@Test
	public void findByUser(){
		User user = userService.findUniqueBy("id", 13l);
		System.out.println(user.getUserName());
	}

}
